import sys, os, time;

user_name = "svilen";
remote_dir = "/projectsloc/"+user_name+"/vldb2008_1/exp";
machineid_file = "machine_id.txt";
expid_file = "exp_id.txt";
expidseq_file = "expid_seq.txt";
systemlog_file = "system.log";
startuplog_file = "startup.log";

machine_list = [];


def get_timestamp():
	return time.strftime("%Y/%m/%d %H:%M:%S", time.localtime());


def system_cmd(cmd):
	f = open(systemlog_file, "a");
	f.write(get_timestamp()+": "+cmd+"\n");
	f.close();

	os.system("nice "+cmd);


def startup_log():
	f = open(startuplog_file, "a");
	f.write(get_timestamp()+": ");
	for i in range(1, len(sys.argv)):
		f.write(sys.argv[i]+" ");
	f.write("\n");
	f.close();


def read_config(file_name):
	dictionary = {};
	try:
		f = open(file_name, "r");
		ll = f.readlines();
		for l in ll:
			l1 = (l.split('\n')[0]).split('=');
			dictionary[l1[0]] = l1[1];			
		f.close();
	except:
		dictionary = {};

	return dictionary;


def write_config(file_name, dictionary):
	f = open(file_name, "w");
        for k, v in dictionary.iteritems():
        	f.write(k+"="+v+"\n");
        f.close();


def read_machinefile():
	f_machines = open("machines.txt", "r");
	ll = f_machines.readlines();
	f_machines.close();

	proc_index = 0;
	for l in ll:
	        s = l.split(' ');
	        s[2] = s[2].split('\n')[0];
		machine_list[proc_index:proc_index] = [[s[0],  int(s[1]), int(s[3]), int(s[2])]];
		proc_index = proc_index+1;


def reset_dirs():
        machine_info = {};

	for machine_name, localproc_count, enabled, proc_share in machine_list:
		if ((enabled == 1) & (localproc_count > 0)):
			print("Resetting "+machine_name);
		
			system_cmd("ssh "+machine_name+" nice rm -R "+remote_dir);
			system_cmd("ssh "+machine_name+" mkdir -p "+remote_dir);

			system_cmd("scp ~/exp/trees.tgz ~/exp/top.tgz "+user_name+"@"+machine_name+":"+remote_dir);	
			system_cmd("ssh "+machine_name+" 'cd "+remote_dir+"; nice tar -xf trees.tgz -z; nice tar -xf top.tgz -z'");			

			machine_info["machine_name"] = machine_name;
			write_config(machineid_file, machine_info);
			system_cmd("scp "+machineid_file+" "+user_name+"@"+machine_name+":"+remote_dir);
			system_cmd("rm "+machineid_file);


def reset_procs():
	machine_info = read_config(remote_dir+"/"+machineid_file);
	try:
		thismachine_name = machine_info["machine_name"];
		print("This machine name: "+thismachine_name);
	except:
		thismachine_name = "";
		print("No machine_id file found");

        proc_index = 0; do_reset = 0;
        for machine_name, localproc_count, enabled, proc_share in machine_list:
                if ((enabled == 1) & (localproc_count > 0)):
                        print("Machine: "+machine_name+", procs: "+str(localproc_count)+", enabled: "+str(enabled));
			if (machine_name != thismachine_name):
				system_cmd("ssh "+machine_name+" 'python ~/utils/reset_procs.py; screen -wipe'");
			else:
				do_reset = 1;


	if ((machine_name != "") & (do_reset == 1)):
		print("Resetting processes on this machine");
		system_cmd("python ~/utils/reset_procs.py");
		system_cmd("screen -wipe");


def start_experiment():
	if (len(sys.argv) < 5):
		print("Expected script_name, start_proc and proc_count");
		sys.exit();

	exp_script = sys.argv[4];
	try:
		f_script = open(exp_script, "r");
		f_script.close();
	except:
		print("Cannot open file "+exp_script);
		sys.exit();

	argproc_index = int(sys.argv[2]);
	argproc_count = int(sys.argv[3]);

	try:
		f_expidseq = open(expidseq_file, "r");
		l = f_expidseq.readlines();
		f_expidseq.close();
		exp_id = int(l[0].split('\n')[0])+1; 
	except:
	 	exp_id = 1;
	
	f_expidseq = open(expidseq_file, "w");
	f_expidseq.write(str(exp_id));
	f_expidseq.close();	

	proc_count = 0;
        for machine_name, localproc_count, enabled, proc_share in machine_list:
                if ((enabled == 1) & (localproc_count > 0)):
                        proc_count = proc_count+localproc_count*proc_share;
        print("Active processes: "+str(proc_count));

	if (argproc_count == 0):
		argproc_count = proc_count;
	if (argproc_index < 0):
		argproc_index = 0;
	if (argproc_index >= proc_count):
		argproc_index = proc_count-1;
	startproc_index = argproc_index;
	endproc_index = argproc_index+argproc_count-1;
	if (endproc_index >= proc_count):
		endproc_index = proc_count-1;
	print("Start process index: "+str(startproc_index)+", end process index: "+str(endproc_index));

        exp_info = {};
        exp_info["exp_id"] = str(exp_id);
	exp_info["percent_complete"] = '0';
        exp_info["proc_count"] = str(endproc_index-startproc_index+1);
        for i in range(5, len(sys.argv)):
		exp_info["param"+str(i-4)] = sys.argv[i];

	proc_index = 0;
	proc_label = 0;
        for machine_name, localproc_count, enabled, proc_share in machine_list:
                if ((enabled == 1) & (localproc_count > 0)):
			exp_info["machine_name"] = machine_name;
			exp_info["proc_share"] = str(proc_share);

                        for p in range(0, localproc_count):				
				if ((proc_index >= startproc_index) & (proc_index <= endproc_index)):
					print("Machine: "+machine_name+", procs: "+str(localproc_count)+", enabled: "+str(enabled)+", share:"+str(proc_share));

					exp_info["proc_index"] = str(proc_index-startproc_index);
					exp_info["proc_label"] = str(proc_label);
					write_config(expid_file, exp_info);

					remoteproc_dir = remote_dir+"/exp"+str(exp_id)+"/proc"+str(proc_label);
                        	        system_cmd("ssh "+machine_name+" mkdir -p "+remoteproc_dir);
                                	system_cmd("scp "+expid_file+" "+user_name+"@"+machine_name+":"+remoteproc_dir);
					system_cmd("rm "+expid_file);
					system_cmd("scp "+exp_script+" "+user_name+"@"+machine_name+":"+remoteproc_dir);
					system_cmd("ssh -t "+machine_name+" 'cd "+remoteproc_dir+"; screen -L time nice python "+exp_script+"'");

				proc_index = proc_index+proc_share;
				proc_label = proc_label+1;


#--------------------------------------------MAIN----------------------------------------------
if (len(sys.argv) < 2):
	print("Need to specify option");
	sys.exit();

startup_log();
read_machinefile();

if (sys.argv[1] == "resetdir"):
	reset_dirs();
elif (sys.argv[1] == "resetproc"):
	reset_procs();
elif (sys.argv[1] == "startexp"):
	start_experiment();
else:
	print("Invalid option");
